﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using t01crm.Data;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Entity;


namespace t01crm.View
{
    /// <summary>
    /// Interaction logic for NewProfile.xaml
    /// </summary>
    public partial class CatalogMaintain : UserControl
    {
       // readonly CatalogMaintainViewModel _catalogTree;  

        CatalogMaintainViewModel cmvavm;
        public CatalogMaintain()
        {
            InitializeComponent();
            cmvavm = Resources["TheViewModel"] as CatalogMaintainViewModel;
            // Get raw family tree data from a database.
            //HG_Prod_Catalog rootcatalog = CatalogMaintainViewModel.GetTree();

            //// Create UI-friendly wrappers around the 
            //// raw data objects (i.e. the view-model).
            //_catalogTree = new CatalogMaintainViewModel(rootcatalog);
           
            // Let the UI bind to the view-model.
     //       base.DataContext = _catalogTree;
          
        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            if (tbProdFatherID.Text != "0" && tbProdFatherID.Text!="")
            {
                MessageBox.Show("二层节点不能再增加子节点");
                return;
            }
            if (tbProdID.Text == "") { tbProdFatherID_MT.Text = "0"; }
            else
            {
                tbProdFatherID_MT.Text = tbProdID.Text;
            }
      
            txtProdName_MT.Text = "";
            tbProdID_MT.Text = "";
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (tbProdFatherID_MT.Text == "" || txtProdName_MT.Text.Trim() == "")
            {
                MessageBox.Show("值无效请重新设定。");
                return;
            }
            try
            {
                string FatherTemp = tbProdFatherID_MT.Text;
                string NameTemp = txtProdName_MT.Text;
                string IDTemp = tbProdID_MT.Text;
               // CatalogMaintainViewModel cmvavm = Resources["TheViewModel"] as CatalogMaintainViewModel;
                cmvavm.SaveNode(Convert.ToInt32(FatherTemp), tbProdID_MT.Text == "" ? 0 : Convert.ToInt32(tbProdID_MT.Text), txtProdName_MT.Text);
                
                
                //保存后更新树及定位树节点
                tvCatalog.ItemsSource = cmvavm.FirstGeneration;
                
                long CurrentNode = 0;

                    if (FatherTemp == "0") //修改一层目录或新增一层目录
                    {   
                        //如果是新增
                        if(IDTemp!="")   CurrentNode = Convert.ToInt32(IDTemp);
                    }
                    else
                    {  //修改二层目录
                        CurrentNode = Convert.ToInt32(FatherTemp);
                    }

                    if (CurrentNode !=0) //如果不是新增一层目录,显示和展开节点
                    {
                        foreach (CatalogViewModel cvm in cmvavm.FirstGeneration)
                        {
                            if (cvm.ID == CurrentNode)
                            {
                                cvm.IsSelected = true;
                                cvm.IsExpanded = true;

                            }
                        }
                    }


                //清除修改位
                    tbProdFatherID_MT.Text = txtProdName_MT.Text = tbProdID_MT.Text = "";

                    MessageBox.Show("成功保存节点：" + NameTemp);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.InnerException.Message);
            }
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            //object temp=tvCatalog.ItemsSource;
            //tvCatalog.ItemsSource = (System.Collections.IEnumerable)temp;
            tbProdFatherID_MT.Text = "";

            tbProdID_MT.Text = "";
            txtProdName_MT.Text = "";

            var treeViewItem = tvCatalog.SelectedItem as CatalogViewModel;
            if(treeViewItem!=null)treeViewItem.IsSelected = false; 


        }

        private void btnModify_Click(object sender, RoutedEventArgs e)
        {
             tbProdFatherID_MT.Text = tbProdFatherID.Text;
            
      
            txtProdName_MT.Text = tbProdName.Text;
            tbProdID_MT.Text = tbProdID.Text;
        }

        private void tvCatalog_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            tbProdFatherID_MT.Text = "";

            tbProdID_MT.Text = "";
            txtProdName_MT.Text = "";
        }
        //private void OnItemSelected(object sender, RoutedEventArgs e)
        //{

        //    tvCatalog.Tag = e.OriginalSource;

        //}

     
    }
}
